package com.example.jetpack.a_viewbinding

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.jetpack.databinding.ActivityViewBindingBinding

class ViewBindingActivity : AppCompatActivity() {

    /**
     * ViewBinding
     */
    private lateinit var binding: ActivityViewBindingBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityViewBindingBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        initFragmentContainerView()
    }

    private fun initFragmentContainerView() {
        val bundle = bundleOf("key" to "first fragment")

        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.run {
            add(binding.fragmentContainerView.id, FirstFragment::class.java, bundle)
            addToBackStack(null)
            commit()
        }
    }

}